<?php
include 'D:/dingjiangbbs/head.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);

//定义文件路径
$web_site   = WEB_SITE;
$admin_home = WEB_SITE.'admin/home/';
$views_home = WEB_SITE.'views/home/';
$admin_2    = WEB_SITE.'admin/admin/';

/*************购买帖子操作结束*********************/

//获取用户的uid和帖子id
$uid = $_COOKIE['uid'];

//此处id可以用$_GET(分页处的title传的)【肯定有】
$id = $_GET['id'];
//var_dump($uid);
//var_dump($id);

/***************计算查看数***********************/	
$table  = DB_TABLE_DETAIL;
$fields = 'hits';
$where  = "where id=$id"; 
$res_hits = select($link,$table,$fields,$where);
//var_dump($res_hits);
$hits = $res_hits[0]['hits'];
//var_dump($hits);
//查看数加一，并插入数据库
//var_dump($hits+1);
$data = ['hits' => $hits+1];
//var_dump($data);
$where = "id=$id";
$hits = update($link,$table,$data,$where);
//var_dump($hits);

/****************************************************/
$tid = $_GET['tid'];
//var_dump($tid);

//根据  添加的 帖子id 找到帖子的相关信息
$table  = DB_TABLE_DETAIL;
$fields	= 'title,content,addtime,replycount,hits,classid,authorid,id,istop,ishot,isdel';
$where  = "where isdel=0 and id=$id";
$res_person2 = select($link,$table,$fields,$where);
//var_dump($res_person2);
//var_dump($res_person2[0]['id']);


//根据uid=uid查询用户名username
$table  = DB_TABLE_USER;
$fields = 'username,usertype,picture,grade,levname';
$where  = "where uid=$uid";
$res_person1 = select($link,$table,$fields,$where);
//var_dump($res_person1);

/*
$grade0 = $res_person1[0]['grade'];
//var_dump($grade0);

//根据用户积分判断用户等级
for ($m=1;$m<=10;$m++) {
			
	if ($grade0 >= $m-100  && $grade0 < $m.'00') {

		$levname = 'LV'.$m;
		//var_dump($levname);
		break;
	}
}    */

$levname = $res_person1[0]['levname'];
//var_dump($levname);

$res_lev2 = select($link,DB_TABLE_LEVEL,'*',"where levname='$levname'");
//var_dump($res_lev2);
//var_dump($res_lev2[0]['lpicture']);




/*****************************************************/
/***************分页显示***********=*****************/

//获取page的初值
//var_dump($_GET['page']);
$page   = isset($_GET['page'])?$_GET['page']:1;

//$page   = ($page>$pageTotal || $page<1)?1:$page; (此句有bug慎用)
//var_dump($page);


//查询获取总条数
$table  = DB_TABLE_DETAIL.' where tid='.$id;
$fields = id;
$total  = idCount($link,$table,$fields,$where);
//var_dump($total);

//每页显示条数
$num    = 4; 

//共多少页
$pageTotal = ceil($total/$num);

//偏移量
$offset = $num * ($page - 1);

//根据每页显示数和偏移量查询
//$sql = select title addtime authorid from bbs_detail limit $offset,$num;

/**********遍历,显示出回复的 所有内容***********/

//根据 tid=$id 遍历出某个帖子的所有回复的信息 (正序)
//根据 tid=$id 查回复者 的 username
$table  = DB_TABLE_DETAIL;
$fields	= 'title,content,addtime,replycount,hits,classid,authorid,tid,id';
$where  = "where tid=$id and first=0 order by addtime limit $offset,$num";
$res_person3 = select($link,$table,$fields,$where);
//var_dump($res_person3);

//循环出回复的全部信息
for ($i=0;$i<count($res_person3);$i++) {

	//循环出回复者的authorid
	$authorid = $res_person3[$i]['authorid'];
	//var_dump($authorid);

	//根据uid=uid查询用户名username
	$table  = DB_TABLE_USER;
	$fields = 'username,picture,usertype,grade,uid,levname';
	$where  = "where uid=$authorid";
	$res_person4 = select($link,$table,$fields,$where);
	//var_dump($res_person4);
	
	$rows4[] = $res_person4;
	//var_dump($rows4);
	
	/****************回复的等级**************************/
	$levname4 = $res_person4[0]['levname'];
	//var_dump($levname4);
	$res_lev4 = select($link,DB_TABLE_LEVEL,'*',"where levname='$levname4'");
		//var_dump($res_lev4);
		//var_dump($res_lev4[0]['lpicture']);
		$rows5[] = $res_lev4;
		//var_dump($rows5);
	/******************************************/

}

//var_dump($rows4);
//var_dump($rows5);


/*****************************************************/
/***************分页显示***********=*****************/

//首、上、下、尾页
$prev = $page - 1;
$next = $page + 1;
if ($prev < 1) {
	$prev = 1;
}
if ($next > $pageTotal) {
	$next = $pageTotal;
}
//var_dump($num,$offset,$page,$total,$prev,$next,$pageTotal);

/*****************分页结束*****************************/
//display传值
$vars = compact('id','web_site','admin_home','views_home','res_person1','res_person2','res_person3','rows4','rows5','num','offset','page','total','prev','next','pageTotal','res','admin_2','res_lev2','levname','res_lev4','levname4');

display('personal.html',$vars);
include DOC_ROOT.'bottom.php';